4.6.1 Lists
TBoS p.63-73, SD
Lists in Shen are produced by placing the items in square brackets. The list of the numbers 1, 2 and 3
is just [1 2 3]. 'head' and 'tail' take the head and tail of a list.
(0-) [1 (+ 1 1) (+ 1 2)]
[1 2 3]
(1-) (head [1 2 3])
1
(2-) (tail [1 2 3])
[2 3] |
'cons' adds an element to the front of a list. Shen includes an infix | which works in the manner of Prolog;
'[1 2 | [3]]' conses 1 and 2 to the list [3]. If the second argument to 'cons' is not a list then the result is a dotted
pair in Lisp jargon.
(0-) (cons 1 [2 3])
[1 2 3]
(1-) [1 2 | [3]]
[1 2 3]
(2-) [1 | (+ 1 1)]
[1 | 2] |
|